import 'package:flutter/material.dart';
import 'package:flutter_getx_project/main/main_controller.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:x_core/x_core.dart';
import 'package:x_project/x_project.dart';

/// Created by XieXin on 2020/3/20.
/// 主页
class MainPage extends GetView<MainController> {
  final state = Get.find<MainController>().state;

  MainPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    controller.initTab();
    return DoubleTapBackExitApp(
      child: Scaffold(
        body: PageView(
          controller: controller.pageController,
          onPageChanged: (index) => controller.setTabIndex(index),
          physics: const NeverScrollableScrollPhysics(),
          children: state.tabPages,
        ),
        bottomNavigationBar: GetBuilder<MainController>(
          builder: (controller) {
            return BottomNavigationBar(
              backgroundColor: Get.theme.bottomAppBarColor,
              items: _buildBottomNavigationBarItem(),
              type: BottomNavigationBarType.fixed,
              currentIndex: state.index,
              elevation: 5.h,
              iconSize: 45.w,
              selectedIconTheme: IconThemeData(color: Get.theme.primaryColor),
              selectedFontSize: 22.sp,
              unselectedFontSize: 22.sp,
              selectedItemColor: Theme.of(context).primaryColor,
              unselectedItemColor: XColors.textColor99,
              onTap: (index) {
                if (index == 1) {
                  //购物车和我的未登录需要跳转到登录页面
                  controller.setTabIndex(index);
                  // if (AccountUtils.isLogin) {
                  //   controller.setTabIndex(index);
                  // } else {
                  //   Get.toNamed(CommonRouter.login);
                  // }
                } else {
                  controller.setTabIndex(index);
                }
              },
            );
          },
        ),
      ),
    );
  }

  List<BottomNavigationBarItem> _buildBottomNavigationBarItem() {
    return List.generate(state.tabTitles.length, (i) {
      return BottomNavigationBarItem(
        icon: Padding(
          padding: EdgeInsets.only(bottom: 3.w),
          child: state.tabIcons[i][0],
        ),
        activeIcon: Padding(
          padding: EdgeInsets.only(bottom: 3.w),
          child: state.tabIcons[i][1],
        ),
        label: state.tabTitles[i],
      );
    });
  }
}
